
package com.work.db.entity;

import java.io.Serializable;
import java.sql.Time;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.*;
import org.hibernate.annotations.*;
import org.hibernate.annotations.CascadeType;

import com.work.util.AppUtils;

@Entity
@Table(name = "PRESENTS")
public class Presents extends BaseEntity implements Serializable {

	@Transient
	private static final long serialVersionUID = 3531824783128587871L;

	//@CorrectTimeFormat
	@NotNull
	@Column(name = "WORK_BEGIN", columnDefinition = "TIME")
	private Time workBegin;

	@NotNull
	@Column(name = "WORK_END", columnDefinition = "TIME")
	private Time workEnd;

	@NotNull
	@Column(name = "LUNCH_BEGIN", columnDefinition = "TIME")
	private Time lunchBegin;

	@NotNull
	@Column(name = "LUNCH_END", columnDefinition = "TIME")
	private Time lunchEnd;

	@Column(name = "MINUTES")
	private Integer minutes;

	@OneToOne(optional = true)
	@Cascade(value = {CascadeType.ALL})
	private WorkDay workDay;

	public Presents() {
		super();
	}

	public Time getWorkBegin()
	{
		return workBegin;
	}

	public void setWorkBegin(Time wokrBegin)
	{
		this.workBegin = wokrBegin;
	}

	public Time getWorkEnd()
	{
		return workEnd;
	}

	public void setWorkEnd(Time workEnd)
	{
		this.workEnd = workEnd;
	}

	public Time getLunchBegin()
	{
		return lunchBegin;
	}

	public void setLunchBegin(Time lunchBegin)
	{
		this.lunchBegin = lunchBegin;
	}

	public Time getLunchEnd()
	{
		return lunchEnd;
	}

	public void setLunchEnd(Time lunchEnd)
	{
		this.lunchEnd = lunchEnd;
	}

	public WorkDay getWorkDay()
	{
		return workDay;
	}

	public void setWorkDay(WorkDay workDay)
	{
		this.workDay = workDay;
	}

	public Integer getMinutes()
	{
		return minutes;
	}

	public void setMinutes(Integer minutes)
	{
		this.minutes = minutes;
	}

	public int calculatePresentsTime()
	{
		if (AppUtils.checkNull(getWorkBegin(), getWorkEnd(), getLunchBegin(), getLunchEnd())) {
			long lunch = getLunchEnd().getTime() - getLunchBegin().getTime();
			long timeInWork = getWorkEnd().getTime() - getWorkBegin().getTime();
			return (int) ((timeInWork - lunch) / AppUtils.MINUTE_MILLIS);
		}
 		return 0;
	}

	public static Presents emptyInstance() {
		Presents instance = new Presents();
		instance.setLunchBegin(AppUtils.createTime(0, 0, 0));
		instance.setLunchEnd(AppUtils.createTime(0, 0, 0));
		instance.setWorkBegin(AppUtils.createTime(0, 0, 0));
		instance.setWorkEnd(AppUtils.createTime(0, 0, 0));
		
		return instance;
	}
	
	@Override
	public String toString()
	{
		return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
			.append("workBegin", getWorkBegin())
			.append("workEnd", getWorkEnd())
			.append("lunchBegin", getLunchBegin())
			.append("lunchEnd", getLunchEnd())
			.append("minutes", getMinutes())
			.toString();
	}
	
}
